<?php

namespace app\admin\controller;

class Document extends Home
{
    /**
     * 文档分类
     * @return mixed
     */
    public function documentCates()
    {
        $list = Db('document_category')->where('pid', 0)->select();
        $this->assign('_list', $list);

        $this->assign('title', '文档分类列表');
        return $this->fetch('cates');
    }

    /**
     * 编辑或新增栏目
     * @return mixed
     */
    public function editCate()
    {
        $id = input('id');

        if (request()->isPost()) {
            $data = request()->post();
            $data['update_time'] = time();
            if ($id) {
                unset($data['id']);
                $res = Db('document_category')->where('id', $id)->update($data);
            } else {
                $data['create_time'] = time();
                $res = Db('document_category')->insert($data);
            }

            if ($res) {
                $this->success('操作成功！', url('documentCates'));
            } else {
                $this->error('操作失败！');
            }
        } else {
            if ($id) {
                $cate = Db('document_category')->find($id);
                $title = '编辑';
            } else {
                $cate = [];
                $title = '新增';
            }

            $this->assign('cateInfo', $cate);
            $this->assign('title', $title);
            return $this->fetch('editcate');
        }
    }

    /**
     * 删除栏目
     */
    public function delCate()
    {
        if (request()->isPost()) {
            $ids = I('post.id');
            if (is_array($ids)) {
                $flag = true;
                foreach ($ids as $id) {
                    $documents = M('document')->where(array('category_id' => $id))->select();
                    if ($documents) {
                        $this->error('栏目下有文章，请先删除文章！');
                    }
                    $res = Db('document_category')->delete($id);
                    if (!$res) {
                        $flag = false;
                    }
                }
                if ($flag) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }
        } else {
            $id = input('id');
            $documents = Db('document')->where(array('category_id' => $id))->select();
            if ($documents) {
                $this->error('该栏目下有文章，请先删除文章！');
            }
            $res = Db('document_category')->delete($id);
            if ($res) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }

    /**
     * 文章列表
     * @return mixed
     */
    public function documents()
    {
        $list = Db('document')->paginate(20);
        $page = $list->render();
        $this->assign('_list', $list);
        $this->assign('page', $page);

        $this->assign('title', '文章列表');
        return $this->fetch();
    }

    /**
     * 编辑或新增文章
     * @return mixed
     */
    public function editDocument()
    {
        $id = input('id');

        if (request()->isPost()) {
            $data = request()->post();
            $data['update_time'] = time();
            if ($id) {
                unset($data['id']);
                $res = Db('document')->where('id', $id)->update($data);
            } else {
                $data['create_time'] = time();
                $res = Db('document')->insert($data);
            }

            if ($res) {
                $this->success('操作成功！', url('documents'));
            } else {
                $this->error('操作失败！');
            }
        } else {
            $document = $id ? Db('document')->find($id) : ['status' => 1];
            $title = $id ? '编辑' : '新增';

            $linksArr = explode(',', $document['links']);
            if (count($linksArr) > 0) {
                foreach ($linksArr as $key => $val) {
                    $linksArr[$key] = get_cover($val, 'path');
                }
            }

            $document['links_arr'] = json_encode(array_filter($linksArr));

            $cates = Db('document_category')->where('pid', 0)->where('status', 1)->select();
            $this->assign('cates', $cates);
            $this->assign('docInfo', $document);
            $this->assign('title', $title);
            return $this->fetch('editdocument');
        }
    }

    /**
     * 删除文章
     */
    public function delDocument()
    {
        if (request()->isPost()) {
            $ids = I('post.id');
            if (is_array($ids)) {
                $flag = true;
                foreach ($ids as $id) {
                    $documents = M('document')->where(array('id' => $id))->select();
                    if (!$documents) {
                        $this->error('有文章不存在！');
                    }
                    $res = Db('document')->delete($id);
                    if (!$res) {
                        $flag = false;
                    }
                }
                if ($flag) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }
        } else {
            $id = input('id');
            $document = Db('document')->where(array('id' => $id))->find();
            if (!$document) {
                $this->error('该文章不存在！');
            }
            $res = Db('document')->delete($id);
            if ($res) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }

    /**
     * 动态列表
     * @return mixed
     */
    public function dynamicLists()
    {
        $list = Db('dynamic')->select();
        $this->assign('_list', $list);

        $this->assign('title', '动态列表');
        return $this->fetch('dynamiclists');
    }

    /**
     * 新增动态
     * @return mixed
     */
    public function addDynamic()
    {
        if (request()->isPost()) {
            $data = request()->post();
            $data['create_time'] = time();
            $res = Db('dynamic')->insert($data);

            if ($res) {
                $this->success('操作成功！', url('dynamicLists'));
            } else {
                $this->error('操作失败！');
            }
        } else {
            $this->assign('title', '新增动态');
            return $this->fetch('adddynamic');
        }
    }

    /**
     * 删除动态
     */
    public function delDynamic()
    {
        $id = input('id');

        $dynamic = Db('dynamic')->where('id', $id)->find();
        if (!$dynamic) {
            $this->error('该动态不存在！');
        }

        $res = Db('dynamic')->delete($id);

        if ($res) {
            $this->success('操作成功！', url('dynamicLists'));
        } else {
            $this->error('操作失败！');
        }
    }
}